const mixin = {//通用部分
    computed: {
        Type() {
            return function (row) {
                if(row.Refund_Type&&row.Refund_Type.charAt(1) != '2'){
                    let str = row.Refund_Type.charAt(1)
                    switch (str) {
                        case "0":
                            return ["退款中","danger",false];
                        case "1":
                            return ["已退款","danger",false];
                        default:
                            return ""
                    }
                }else {
                    let str = row.Row_Status
                    switch (str) {
                        case "0":
                            return ["待发货","",true];
                        case "1":
                            return ["发货中","warning",true];
                        case "2":
                            return ["已发货","success",true];
                        default:
                            return ""
                    }
                }   
            }
        }
    }
}
Vue.component('yl-button', {
    template: `
    <span>
        <el-button :type="type" @click="Export" :loading="loading">
            <slot></slot>
        </el-button>
        <el-table id="ExportData" :data="data" style="display: none; width: 100%;margin-bottom: 20px;">
            <el-table-column align="center" prop="Sales_Order_Detail_SerialNumber"  label="订单明细编号"></el-table-column>
            <el-table-column align="center" prop="Sales_Order_SerialNumber" label="订单编号"></el-table-column>
            <el-table-column align="center" prop="Article_Name" label="商品名称"></el-table-column>
            <el-table-column align="center" prop="Specifications_Name" label="规格名称"></el-table-column>
            <el-table-column align="center" prop="Sales_Date" label="下单时间"></el-table-column>
            <el-table-column align="center" prop="Quantity" label="共发货总量"></el-table-column>
            <el-table-column align="center" prop="Delivery_Quantity" label="已发货数量"></el-table-column>
            <el-table-column align="center" label="剩余发货数量">
                <template v-slot="{row}">
                    {{parseInt(row.Quantity)-parseInt(row.Delivery_Quantity)}}
                </template>
            </el-table-column>
            <el-table-column align="center" prop="Row_Status" label="发货状态">
                <template v-slot="{row}">
                    <el-tag>{{Type(row)[0]}}</el-tag>
                </template>
            </el-table-column>
        </el-table>
    </span>`,
    props: ['loading', 'data', 'type'],
    mixins:[mixin],
    methods: {
        Export() {
            this.$emit('click')
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "firstparty_sales_order_detail",
                PaginationNumber: 1,//页数
                RowAmount: 20,//每页数据条数
                Condition: '',
                ExtensionCondition: '',
                Department_SerialNumber:[],
                Sales_Order_SerialNumber: '',
                Sales_Date: null,
            },
            total: 0,
            tableData: [],	//表格数据
            ExportData: [],//导出
            loading: false,
            dialogVisible: false,//物流
            dialogVisible1:false,//明细
            Department_SerialNumber:'',
        }
    },
    mounted() {
        if (getCookie('role')) {
            let roleObj = JSON.parse(getCookie('role'));
            this.Department_SerialNumber = roleObj.other;
            this.getData(roleObj.other,(data)=>{
                this.form.Department_SerialNumber = data
                this.getList(this.form);//前端分页
            })
        }
    },
    mixins:[mixin],
    computed: {
        Color(){
            return function (row) {
                if ((parseInt(row.Quantity) - parseInt(row.Delivery_Quantity))==0) {
                    return "#67C23A"
                } else if (parseInt(row.Delivery_Quantity)==0) {
                    return "#F56C6C"
                }else if ((parseInt(row.Quantity) - parseInt(row.Delivery_Quantity)) > 0){
                    return "#E6A23C"
                }
            }
        }
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getData(Department_SerialNumber,callback){
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/getAllDepartmentByUserName?UserName=&ParentNumber=${Department_SerialNumber}`,
                type: 'GET',
                success: function (data) {
                    let arr2 = []
                    that.recursion(data, 'Department_SerialNumber', arr2);
                    callback([...arr2,Department_SerialNumber])
                },
                error:function(){
                    callback([])
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        recursion(arr1, str, arr2) {
            arr1.forEach(item => {
                arr2.push(item[str]);
                if (item.Children && item.Children.length > 0) {
                    this.recursion(item.Children, str, arr2);
                }
            });
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            // if (data.Sales_Date) {
            //     sql = sql + "AND business_sales_order.Sales_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            // }
            if (data.Department_SerialNumber.length>0) {
                let str = "";
                data.Department_SerialNumber.forEach(item=>{
                    if (str) {
                        str = str + "OR business_member_department.Department_SerialNumber = '"+ item +"'\r\n";
                    }else{
                        str = "business_member_department.Department_SerialNumber = '"+ item +"'\r\n";
                    }
                })
                sql = sql + " and (" + str + ")"
            }
            console.log(sql);
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        handleLogistics(command,row) {//物流信息
            if (command=="Logistics") {
                this.Sales_Order = row;
                this.dialogVisible = true;
            }
        },
        downloadTemplate(command) {
            if (command == "logistics") {
                window.location.href = './download/发货物流信息模板.xlsx';
            }
        },
        importTemplate(command) {
            if (command == "logistics") {
                window.location.href = './import.html';
            }
        },
        Export(form) {//导出
            const that = this;
            this.loading = true;
            let data = deepClone(form);
            data.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Sales_Order_SerialNumber + "%,%" + data.Sales_Order_SerialNumber + "%")))
            let sql = "";
            if (data.Sales_Date) {
                sql = sql + "AND business_sales_order.Sales_Date BETWEEN '" + data.Sales_Date[0] + "' AND '" + data.Sales_Date[1] + "'\r\n"
            }
            if (data.Supplier_SerialNumber) {
                sql = sql + "AND business_sales_order_detail.Supplier_SerialNumber = '" + data.Supplier_SerialNumber + "'\r\n"
            }
            data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    if (Result.length > 0) {
                        that.ExportData = Result;
                        that.$nextTick(() => {
                            let wb = XLSX.utils.table_to_book(document.querySelector("#ExportData"));
                            var wbout = XLSX.write(wb, {
                                bookType: "xlsx",
                                bookSST: true,
                                type: "array"
                            });
                            try {
                                saveAs(
                                    new Blob([wbout], {
                                        type: "application/octet-stream"
                                    }),
                                    "发货物流信息表" + (+new Date()) + ".xlsx"
                                );
                            } catch (e) {
                                if (typeof console !== "undefined") console.log(e, wbout);
                            }
                            that.$message({
                                message: '已成功导出',
                                type: 'success'
                            })
                            return wbout;
                        })
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    },
})